Skip to content

docs: Add warning about dangling references with lazy reducers (fixes #2871)#2882

Open
ssam18 wants to merge 3 commits intoxtensor-stack:masterfrom
ssam18:master
Open

docs: Add warning about dangling references with lazy reducers (fixes #2871)#2882
ssam18 wants to merge 3 commits intoxtensor-stack:masterfrom
ssam18:master

Conversation

@ssam18
Copy link
Copy Markdown

@ssam18 ssam18 commented Dec 18, 2025

This PR addresses GitHub issue #2871 where using auto with xtensor reducer functions (like amax, sum with keep_dims) causes crashes or incorrect results in optimized builds, particularly on Android ARM64.

Root Cause: Lazy expressions hold references to local variables. When intermediate results are stored with auto, these references become dangling when the function returns, leading to undefined behavior.

Changes:

  • docs/source/pitfall.rst: Added detailed warning section explaining the issue and providing three solutions:
    1. Use explicit container types (xt::xtensor<T, N>)
    2. Use xt::eval() to force immediate evaluation
    3. Use evaluation_strategy::immediate with reducers
  • test/test_xmath.cpp: Added test demonstrating correct patterns

Testing:

  • All 83 xmath tests pass
  • All 64 xreducer tests pass

Fixes #2871

…tensor-stack#2871)

This commit addresses GitHub issue xtensor-stack#2871 where using 'auto' with xtensor
reducer functions (like amax, sum with keep_dims) in functions causes
crashes or incorrect results in optimized builds.

The issue is that lazy expressions hold references to local variables.
When intermediate results are stored with 'auto', these references
become dangling when the function returns, leading to undefined behavior.

Fixes xtensor-stack#2871
@ssam18
Copy link
Copy Markdown
Author

ssam18 commented Apr 2, 2026

@kou @minrk @matwey @jmp75 - Please review this PR.

- Only force evaluation of the returned expression; intermediate lazy
  expressions are safe as long as they do not outlive the function
- Use auto for shifted/expVals/sumExp in the doc example and tests
- Remove misleading "Alternatively" sections (eval on intermediates is
  not required; evaluation_strategy::immediate does not fix the issue)
- Fix logSoftmax_eval test variant to drop unnecessary xt::eval wrappers
@ssam18 ssam18 requested a review from JohanMabille April 3, 2026 13:13
A lazy reducer (e.g. xt::sum with keep_dims) cannot be safely used as an
operand in a subsequent element-wise expression without first materializing
it. Element-wise lazy expressions (shifted, expVals) are safe as auto, but
the reducer result (sumExp) must be assigned to xt::xtensor or wrapped in
xt::eval before use.

This fixes the CI test failure in issue_2871_intermediate_result_handling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android ARM64: xtensor auto Deduction Fails in Release Build - Expression Template Optimization Bug

2 participants